package xin.nick.manager;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import xin.nick.config.SystemProperties;
import xin.nick.util.CodingOpenUtil;
import xin.nick.util.domain.AccessTokenResponse;
import xin.nick.util.domain.MeResponse;

/**
 * @author Nick
 * @since 2023/4/4
 */
@Component
@RequiredArgsConstructor
public class CodingOpenManager {

    private final SystemProperties systemProperties;

    /**
     * 获取授权地址
     * @return
     */
    public String getAuthorizeUrl() {

        String teamName = systemProperties.getTeamName();
        String redirectUrl = systemProperties.getRedirectUrl();
        String clientId = systemProperties.getClientId();
        String scope = systemProperties.getScope();

        String authorizeUrl = CodingOpenUtil.getAuthorizeUrl(teamName, clientId, redirectUrl, scope);
        return authorizeUrl;

    }

    /**
     * 根据code获取当前的 AccessToken
     * @param code
     * @return
     */
    public String getAccessToken(String code) {

        String teamName = systemProperties.getTeamName();
        String clientId = systemProperties.getClientId();
        String clientSecret = systemProperties.getClientSecret();

        AccessTokenResponse accessTokenResponse = CodingOpenUtil.getAccessToken(teamName, clientId, clientSecret, code);
        String accessToken = accessTokenResponse.getAccessToken();
        return accessToken;
    }


    /**
     * 获取当前用户信息
     * @param accessToken
     * @return
     */
    public MeResponse getMe(String accessToken) {

        String teamName = systemProperties.getTeamName();
        MeResponse me = CodingOpenUtil.getMe(teamName, accessToken);
        return me;
    }

}
